package zhou.chs.p2_3;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    ListView lv;            //声明1个ListView控件
    int smax=24;                          //数据组数
    String[] strhl = new String[smax] ;   //字符串数组
    int[] t1 = new int[smax];            //整形数组
    int[] t2 = new int[smax];            //整形数组
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.idlv);     //得到1个ListView对象
        MyAdapter mAdapter = new MyAdapter(this);//得到1个MyAdapter对象
        lv.setAdapter(mAdapter);        //为ListView绑定Adapter
        //给待显示数组赋值
        for(int i=0;i<smax;i++) strhl[i]= "L" + Integer.toString(i+1);
        t2[4]=32;
    }
    //存放item.xml中的控件
    public final class ViewHolder{
        public TextView tv1;
        public TextView tv2;
        public TextView tv3;
    }
    //构造新的适配器
    private class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;//得到一个LayoutInfalter对象用来导入布局
        //构造函数
        public MyAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }
        @Override   //返回数组的长度
        public int getCount() {
            return smax;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) { return 0; }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item,null);
                holder = new ViewHolder();
                //得到各个控件的对象
                holder.tv1 = (TextView) convertView.findViewById(R.id.idtv1);
                holder.tv2 = (TextView) convertView.findViewById(R.id.idtv2);
                holder.tv3 = (TextView) convertView.findViewById(R.id.idtv3);
                convertView.setTag(holder);//绑定ViewHolder对象
            }
            else{
                holder = (ViewHolder)convertView.getTag();//取出ViewHolder对象
            }
            //设置TextView显示的内容，即我们存放在数组中的数据
            holder.tv1.setText(strhl[position]);
            holder.tv2.setText(Integer.toString(t1[position]));
            holder.tv3.setText(Integer.toString(t2[position]));
            //对数组t2中的数据检查，超出30时数据背景颜色标红
            if(t2[position] > 30) holder.tv3.setBackgroundColor(0xFFFF0000);
            else holder.tv3.setBackgroundColor(0);
            return convertView;
        }
    }
}
